#pragma once
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>

#include "VehicleRegister.h"
#include "Repaired.h"

using namespace std;

class VehicleDataBase
{
private:
	vector<VehicleRegister> db;
	int active;
public:

	VehicleDataBase() : active(0) {}

	~VehicleDataBase(){}

	void Add(VehicleRegister vr)
	{
		db.push_back(vr);
	}

	void SetActive(int active) { this->active = active; }

	string FindVehicles(string brand, int year)
	{
		ostringstream oss;
		for (auto vr : db)
		{
			oss << vr.FindVehicles(brand, year);
		}
		return oss.str();
	}

	void AddVehicle(Vehicle v)
	{
		db[active].Add(v);
	}

	void DeleteVehicle(int i)
	{
		db[active].Delete(i);
	}

	void AddRepair(int i, Repaired r)
	{
		db[active].AddRepair(i, r);
	}

	string ListRegisters()
	{
		ostringstream oss;
		for (int i = 0; i < db.size(); i++)
		{
			oss << i << ": " << db[i].GetName() << endl;
		}
		return oss.str();
	}

	void menu()
	{
		bool runMenu = true;
		while (runMenu)
		{
			system("cls");
			cout << "1: Add Register" << endl;
			cout << "2: Set Active Register" << endl;
			cout << "3: Search For Vehicle" << endl;
			cout << "4: Add Vehicle" << endl;
			cout << "5: Delete Vehicle" << endl;
			cout << "6: Add Repairs" << endl;
			cout << endl << "Anything else: Exit" << endl << endl;

			int selection = 0;
			cin >> selection;
			cin.get();

			//Helper variables
			int i;
			string str;
			Vehicle veh;
			Repaired rep;
			VehicleRegister vr;

			switch (selection)
			{
			case 1: //Add Register
				vr.Read();
				db.push_back(vr);
				break;
			case 2: //Set Active Register
				system("cls");
				cout << ListRegisters();
				cout << endl << "Set which register as active: ";
				cin >> i;
				cin.get();
				if (i >= 0 && i < db.size())
				{
					active = i;
				}
				else
				{
					cout << "invalid selection " << i << endl;
					system("pause");
				}
				break;
			case 3: //Search For Vehicle
				system("cls");
				cout << "Brand: ";
				getline(cin, str);
				cout << "Year: ";
				cin >> i;
				cin.get();
				cout << FindVehicles(str, i);
				system("pause");
				break;
			case 4: //Add Vehicle
				veh.read();
				db[active].Add(veh);
				break;
			case 5: //Delete Vehicle
				cout << db[active].List();
				cout << endl << "Delete which vehicle: ";
				cin >> i;
				cin.get();
				db[active].Delete(i);
				break;
			case 6: //Add Repairs
				cout << db[active].List();
				cout << endl << "Add repairs for which vehicle: ";
				cin >> i;
				cin.get();
				rep.Read();
				db[active].AddRepair(i, rep);
				break;
			default: //exit
				runMenu = false;
				break;
			}
		}

	}
};

